home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / VB Source 20626652001.psc / PrintSource.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-05-25  |  14.8 KB  |  429 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "PrintSource"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. Option Explicit
  15. Option Compare Text
  16.  
  17. ' Module Name:  PrintSource.cls
  18. ' Author:       Santiago A. MΘndez  (Guatemala, C.A.)
  19. ' email:        Santiago@InternetDeTelgua.com.gt
  20. ' Date:         23-Abr-01
  21. ' Description:  The purpose of this Class module is to print the code in the active code pane window of
  22. '               VB IDE.  The way the code is printed is to determine the amount of lines (selected or
  23. '               total module lines) to print, then sends line by line to printer.
  24.  
  25. Private Type CodeLine                   'IS USED TO CLASSIFY THE CODE LINE PRINTED
  26.     IsSub As Boolean                    'LINE IS A SUB DECLARATION
  27.     IsFunction As Boolean               'LINE IS A FUNCTION DECLARATION
  28.     IsEnumType As Boolean               'LINE IS A TYPE DEFINITION
  29.     IsEndProc As Boolean                'LINE IS A END SUB/FUNCTION
  30.     IsEndType As Boolean                'LINE IS A END TYPE DEFINITION
  31.     IsComment As Boolean                'LINE IS A COMMENT LINE ('/Rem)
  32. End Type
  33.  
  34. Private m_PrinterLeft As Single
  35. Private m_PrinterRigth As Single
  36. Private m_PrinterTop As Single
  37. Private m_PrinterBottom As Single
  38. Private m_PrinterHeight As Single
  39. Private m_PrinterWidth As Single
  40.  
  41.  
  42. Public Property Let PrinterBottom(ByVal Data As Single)
  43.     m_PrinterBottom = Data
  44. End Property
  45.  
  46. Public Property Get PrinterBottom() As Single
  47.     PrinterBottom = m_PrinterBottom
  48. End Property
  49.  
  50. Public Property Let PrinterLeft(ByVal Data As Single)
  51.     m_PrinterLeft = Data
  52. End Property
  53.  
  54. Public Property Get PrinterLeft() As Single
  55.     PrinterLeft = m_PrinterLeft
  56. End Property
  57.  
  58. Public Property Let PrinterRigth(ByVal Data As Single)
  59.     m_PrinterRigth = Data
  60. End Property
  61.  
  62. Public Property Get PrinterRigth() As Single
  63.     PrinterRigth = m_PrinterRigth
  64. End Property
  65.  
  66. Public Property Let PrinterTop(ByVal Data As Single)
  67.     m_PrinterTop = Data
  68. End Property
  69.  
  70. Public Property Get PrinterTop() As Single
  71.     PrinterTop = m_PrinterTop
  72. End Property
  73.  
  74. '*--- Function to Print Entire Module or Selected text from current code pane window
  75. Public Sub PrintSourceCode(PrintSelection As Boolean, PrintIndex As Boolean)
  76.     Dim StartLine&, EndLine&, i&, J&, LineaTexto$, sBuffer$, Indice(), Prop As Property
  77.     Dim DeclarationLines&, a&, b&, c&, d&, sBuffer2$, CodeLineType As CodeLine, PadLeft$
  78.     
  79.     Printer.ScaleMode = vbCentimeters         'CM AS UNIT WHEN SETTING CurrentX, CurrentY IN PRINTER OBJECT
  80.     
  81.     'CALCULATE PRINT AREA
  82.     m_PrinterHeight = Printer.ScaleHeight - m_PrinterTop
  83.     m_PrinterWidth = Printer.ScaleWidth - m_PrinterLeft - m_PrinterRigth
  84.     PadLeft = String(m_PrinterLeft / Printer.TextWidth(" "), " ")
  85.     
  86.     'FONT
  87.     Printer.FontName = "Courier New"
  88.     Printer.FontSize = 9
  89.     
  90.     If PrintSelection Then
  91.         VBI.ActiveCodePane.GetSelection StartLine, J, EndLine, i                'GET RANGE OF LINES SELECTED IN WINDOW
  92.     Else
  93.         StartLine = 1
  94.         EndLine = VBI.ActiveCodePane.CodeModule.CountOfLines                    'COUNT OF LINES IN MODULE
  95.     End If
  96.     DeclarationLines = VBI.ActiveCodePane.CodeModule.CountOfDeclarationLines    'DECLARATIONS AT BEGINNING OF MODULE
  97.     
  98.     PrintHeader True
  99.     
  100.     ReDim Preserve Indice(1, 0)
  101.     For i = StartLine To EndLine
  102.         Printer.FontBold = False
  103.         Printer.FontItalic = False
  104.         
  105.         LineaTexto = VBI.ActiveCodePane.CodeModule.Lines(i, 1)   'GET LINE OF TEXT
  106.         sBuffer = Trim(LineaTexto)
  107.         
  108.         CheckLineWidth LineaTexto
  109.         
  110.         'CHECK IF LINE ENDS WITH LINE-CONTINUATION CHARACTER
  111.         If Right(LineaTexto, 1) = "_" Then
  112.             J = i
  113.             Do
  114.                 J = J + 1
  115.                 sBuffer = VBI.ActiveCodePane.CodeModule.Lines(J, 1)
  116.                 
  117.                 CheckLineWidth sBuffer
  118.                 LineaTexto = LineaTexto & vbCr & sBuffer
  119.                 If Right(sBuffer, 1) <> "_" Then Exit Do
  120.             Loop
  121.             i = J
  122.         End If
  123.         sBuffer = Trim(LineaTexto)
  124.         
  125.         'CHECK IF NEXT LINE FITS ON CURRENT PAGE
  126.         If Printer.CurrentY + Printer.TextHeight(LineaTexto) > m_PrinterHeight Then
  127.             Printer.NewPage
  128.             PrintHeader True
  129.         End If
  130.         
  131.         'PRINT TEXT LINE
  132.         PrintTextLine LineaTexto, CodeLineType
  133.         
  134.         If CodeLineType.IsFunction Or CodeLineType.IsSub Then       'IF SUB/FUNCTION ADD TO INDEX
  135.             ReDim Preserve Indice(1, UBound(Indice, 2) + 1)
  136.             
  137.             J = InStr(sBuffer, "(") - 1
  138.             Indice(0, UBound(Indice, 2)) = Left(sBuffer, J)         'SUB/FUNCTION NAME
  139.             Indice(1, UBound(Indice, 2)) = Printer.Page             'PAGE WHERE IS PRINTED
  140.             
  141.         ElseIf CodeLineType.IsEndProc Then                          'IF END OF PROCEDURE PRINT A LINE
  142.             PrintLine
  143.         End If
  144.         
  145.         If i = DeclarationLines Then PrintLine                      'IF END OF DECLARATIONS SECTION PRINT A LINE
  146.     Next
  147.     
  148.     'NOW PRINT INDEX
  149.     If PrintIndex Then
  150.         
  151.         PrintHeaderIndex EndLine - StartLine + 1
  152.         
  153.         'LOOP ARRAY OF PROCEDURES
  154.         For i = 1 To UBound(Indice, 2)
  155.             'CHECK IF NEXT LINE FITS ON CURRENT PAGE
  156.             If Printer.CurrentY + Printer.TextHeight(Indice(0, i)) > m_PrinterHeight Then
  157.                 PrintHeaderIndex EndLine - StartLine + 1
  158.             End If
  159.             
  160.             'Printer.CurrentX = 1
  161.             Printer.CurrentX = m_PrinterLeft + 1
  162.             Printer.Print Indice(0, i);             'SUB/FUNCTION NAME
  163.             ImprimirPuntos
  164.             PrintRight Indice(1, i) & Space(3)      '# PAGE
  165.         Next
  166.     End If
  167.     
  168.     Printer.EndDoc
  169.     
  170. End Sub
  171.  
  172. '*--- Check if line to print is larger than page width, break text line in many lines as needed inserting carriage returns
  173. Private Sub CheckLineWidth(ByRef LineaTexto As String)
  174.     Dim sBuffer$, a&, b&, c&, d&
  175.     
  176.     If Printer.TextWidth(LineaTexto) > m_PrinterWidth Then
  177.         sBuffer = ""
  178.         
  179.         a = Printer.TextWidth(LineaTexto) \ m_PrinterWidth
  180.         
  181.         c = 1
  182.         For b = 1 To a
  183.             Do
  184.                 d = d + 1
  185.             Loop Until Printer.TextWidth(Mid(LineaTexto, c, d - 1)) > m_PrinterWidth
  186.             
  187.             sBuffer = sBuffer & Mid(LineaTexto, c, d - 2) & vbCr
  188.             c = c + d - 2
  189.             d = 0
  190.         Next
  191.         sBuffer = sBuffer & Mid(LineaTexto, c)
  192.         LineaTexto = sBuffer
  193.     End If
  194. End Sub
  195.  
  196. '*--- Print Header of Index Page
  197. Private Sub PrintHeaderIndex(CountLines As Long)
  198.    Dim Prop As Property, sBuffer$
  199.     
  200.     Printer.FontItalic = False
  201.     Printer.NewPage
  202.     PrintHeader False
  203.     
  204.     Printer.CurrentY = m_PrinterTop + 3     '3CM FROM LEFT MARGIN
  205.     
  206.     Printer.FontBold = True
  207.     Printer.CurrentX = m_PrinterLeft + 1
  208.     Printer.Print "Archivo:";
  209.     Printer.FontBold = False
  210.     Printer.CurrentX = m_PrinterLeft + 5
  211.     Printer.Print VBI.ActiveCodePane.CodeModule.Parent.Name;                'MODULE NAME
  212.     Printer.Print
  213.     Printer.FontBold = True
  214.     Printer.CurrentX = m_PrinterLeft + 1
  215.     Printer.Print "Path:";
  216.     Printer.FontBold = False
  217.     Printer.CurrentX = m_PrinterLeft + 5
  218.     
  219.     sBuffer = VBI.ActiveCodePane.CodeModule.Parent.FileNames(1)             'FILE NAME
  220.     
  221.     CheckLineWidth sBuffer
  222.     PrintText sBuffer, False, True
  223.     
  224.     If CountLines > 0 Then
  225.         Printer.FontBold = True
  226.         Printer.CurrentX = m_PrinterLeft + 1
  227.         Printer.Print "Lines of Code:";
  228.         Printer.FontBold = False
  229.         Printer.CurrentX = m_PrinterLeft + 5
  230.         Printer.Print Format(CountLines, "###,##0")
  231.         Printer.Print
  232.     End If
  233.     Printer.Print
  234.     
  235.     On Error Resume Next
  236.     'PRINT PROPERTIES OF CURRENT MODULE ONLY IF IT IS A CLASS MODULE OR MODULE
  237.     If VBI.ActiveCodePane.CodeModule.Parent.Type <= vbext_ct_ClassModule + vbext_ct_ClassModule Then
  238.         For Each Prop In VBI.ActiveCodePane.CodeModule.Parent.Properties
  239.             Printer.CurrentX = m_PrinterLeft + 1
  240.             Printer.Print Prop.Name;        'PROPERTY NAME
  241.             Printer.CurrentX = m_PrinterLeft + 7
  242.             Printer.Print Prop.Value;       'PROPERTY VALUE
  243.             Printer.Print
  244.         Next
  245.     End If
  246.     On Error GoTo 0
  247.     
  248.     Printer.Print       '2 BLANK LINES
  249.     Printer.Print
  250.     
  251.     Printer.FontBold = True
  252.     Printer.FontUnderline = True
  253.     Printer.CurrentX = m_PrinterLeft + 1
  254.     Printer.Print "Procedure";
  255.     PrintRight "Page #"
  256.     Printer.Print
  257.     Printer.FontBold = False
  258.     Printer.FontUnderline = False
  259. End Sub
  260.  
  261. '*--- Print a line of dots (index page)
  262. Private Sub ImprimirPuntos()
  263.     Dim Fin!
  264.     Fin = m_PrinterWidth - 1.5
  265.     
  266.     Printer.Print " ";
  267.     Do
  268.         Printer.Print ".";
  269.     Loop Until Printer.CurrentX >= Fin
  270.     
  271.     Printer.Print " ";
  272. End Sub
  273.  
  274. '*--- Prints Text Right Aligned
  275. Private Sub PrintRight(Texto As String)
  276.     Printer.CurrentX = m_PrinterWidth - Printer.TextWidth(Texto) + m_PrinterLeft
  277.     Printer.Print Texto
  278. End Sub
  279.  
  280. '*--- Prints Page Header, optional prints page number
  281. Private Sub PrintHeader(PrintPageNumber As Boolean)
  282.     Dim sBuffer$, i%
  283.     
  284.     'PRINT DATE, TIME,MODULE NAME AND OPTIONAL PAGE NUMBER
  285.     
  286.     sBuffer = VBI.ActiveCodePane.CodeModule.Parent.Name
  287.     If PrintPageNumber Then sBuffer = sBuffer & "-" & Printer.Page
  288.     sBuffer = Format(Now, "dd-mmm-yyyy HH:mm AM/PM") & Space(10) & sBuffer
  289.     
  290.     With Printer
  291.         .CurrentX = m_PrinterLeft
  292.         .CurrentY = m_PrinterTop
  293.         .FontBold = True
  294.         .FontItalic = True
  295.         PrintRight sBuffer
  296.         
  297.         i = .FontSize
  298.         .FontSize = 2                   'PRINT DOUBLE LINE
  299.         Printer.Line (m_PrinterLeft, .CurrentY)-(m_PrinterWidth + m_PrinterLeft, .CurrentY)
  300.         Printer.Print
  301.         Printer.Line (m_PrinterLeft, .CurrentY)-(m_PrinterWidth + m_PrinterLeft, .CurrentY)
  302.         .FontSize = i \ 2
  303.         Printer.Print
  304.         .FontSize = i
  305.         .FontBold = False
  306.         .FontItalic = False
  307.     End With
  308. End Sub
  309.  
  310. '*--- Print Line of text formatted according to the text of line
  311. Private Sub PrintTextLine(ByVal LineaTexto As String, ByRef RetTypeLine As CodeLine)
  312.     Dim sBuffer$, J%, LineCode As CodeLine
  313.     
  314.     RetTypeLine = LineCode          'TO CLEAR RetTypeLine VALUES
  315.     
  316.     sBuffer = Trim(LineaTexto)
  317.     If Left(sBuffer, 11) = "Private Sub" Or _
  318.         Left(sBuffer, 10) = "Friend Sub" Or _
  319.         Left(sBuffer, 10) = "Static Sub" Or _
  320.         Left(sBuffer, 10) = "Public Sub" Or _
  321.         Left(sBuffer, 3) = "Sub" Then                           'SUB
  322.             RetTypeLine.IsSub = True
  323.     
  324.     ElseIf Left(sBuffer, 16) = "Private Function" Or _
  325.         Left(sBuffer, 15) = "Public Function" Or _
  326.         Left(sBuffer, 15) = "Friend Function" Or _
  327.         Left(sBuffer, 15) = "Static Function" Or _
  328.         Left(sBuffer, 8) = "Function" Then                      'FUNCTION
  329.         
  330.             RetTypeLine.IsFunction = True
  331.     
  332.     ElseIf Left(sBuffer, 20) Like "Private Property [LGS]et" Or _
  333.         Left(sBuffer, 19) Like "Public Property [LGS]et" Or _
  334.         Left(sBuffer, 19) Like "Friend Property [LGS]et" Or _
  335.         Left(sBuffer, 16) Like "Property [LGS]et" Then          'PROPERTY GET/LET/SET
  336.         
  337.         RetTypeLine.IsFunction = True
  338.     
  339.     ElseIf Left(sBuffer, 11) = "Public Type" Or _
  340.             Left(sBuffer, 12) = "Private Type" Then             'TYPE DEFINITION
  341.             RetTypeLine.IsEnumType = True
  342.     
  343.     ElseIf Left(sBuffer, 8) = "End Type" Then                   'END TYPE DEFINITION
  344.             RetTypeLine.IsEndType = True
  345.             
  346.     ElseIf Left(sBuffer, 7) = "End Sub" Or _
  347.             Left(sBuffer, 12) = "End Function" Or _
  348.             Left(sBuffer, 12) = "End Property" Then            'END PROCEDURE DEFINITION
  349.             RetTypeLine.IsEndProc = True
  350.     
  351.     ElseIf Left(sBuffer, 1) = "'" Or Left(sBuffer, 3) = "Rem" Then      'COMMENT
  352.             RetTypeLine.IsComment = True
  353.     End If
  354.     
  355.     'PRINT TEXT CODE LINE
  356.     
  357.     Printer.ForeColor = QBColor(0)             'BLACK COLOR
  358.     
  359.     'IF LINE CONTAINS COMMENTS, BUT IS NOT A ENTIRE COMMENT LINE
  360.     If InStr(sBuffer, " '") Or InStr(sBuffer, "Rem ") And Not RetTypeLine.IsComment Then
  361.         J = InStr(LineaTexto, " '")
  362.         If J = 0 Then J = InStr(LineaTexto, "Rem")
  363.         
  364.         sBuffer = Mid(LineaTexto, 1, J - 1)
  365.         
  366.         'PRINT CODE WITHOUT COMMENT
  367.         Printer.FontItalic = False
  368.         Printer.FontBold = RetTypeLine.IsEndProc Or RetTypeLine.IsEndType Or RetTypeLine.IsEnumType Or _
  369.                             RetTypeLine.IsFunction Or RetTypeLine.IsSub
  370.         PrintText sBuffer, True
  371.         sBuffer = Mid(LineaTexto, J)
  372.         
  373.         'NOW PRINT COMMENT
  374.         Printer.FontBold = True
  375.         Printer.FontItalic = True
  376.         Printer.ForeColor = QBColor(8)             'GRAY COLOR
  377.         PrintText sBuffer, False, True
  378.     Else
  379.         'PRINT ENTIRE LINE
  380.         Printer.FontBold = RetTypeLine.IsComment Or RetTypeLine.IsEndProc Or RetTypeLine.IsEndType Or _
  381.                         RetTypeLine.IsEnumType Or RetTypeLine.IsFunction Or RetTypeLine.IsSub
  382.         Printer.FontItalic = RetTypeLine.IsComment
  383.         If RetTypeLine.IsComment Then Printer.ForeColor = QBColor(8)                        'COMMENTS IN GRAY COLOR
  384.         PrintText LineaTexto, False
  385.     End If
  386. End Sub
  387.  
  388. '*--- Print Lines of Text
  389. Private Sub PrintText(ByVal Texto As String, NoBreakLine As Boolean, Optional ContinueLine As Boolean)
  390.     Dim ArrayLines() As String, i%
  391.     
  392.     If Texto = "" Then
  393.         Printer.Print
  394.     Else
  395.         ArrayLines = Split(Texto, vbCr)
  396.         
  397.         If ContinueLine Then
  398.             ContinueLine = False
  399.         Else
  400.             Printer.CurrentX = m_PrinterLeft
  401.         End If
  402.         
  403.         For i = 0 To UBound(ArrayLines) - 1
  404.             Printer.Print ArrayLines(i)
  405.             Printer.CurrentX = m_PrinterLeft
  406.         Next
  407.         If NoBreakLine Then
  408.             Printer.Print ArrayLines(i);
  409.         Else
  410.             Printer.Print ArrayLines(i)
  411.         End If
  412.     End If
  413. End Sub
  414.  
  415. '*--- Print a line in current printer line
  416. Private Sub PrintLine()
  417.     Dim X As Single, Y As Single, FSize%
  418.     
  419.     With Printer
  420.         FSize = .FontSize
  421.         .FontSize = FSize \ 2
  422.         .FontSize = FSize
  423.         Printer.Line (m_PrinterLeft, .CurrentY)-(m_PrinterWidth + m_PrinterLeft, .CurrentY)
  424.         .FontSize = FSize \ 2
  425.         Printer.Print
  426.         .FontSize = FSize
  427.     End With
  428. End Sub
  429.